package wk.demo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luoye.bzijkplayer.R;

import java.io.File;
import java.util.List;

import wk.demo.bean.DirFiles;
import wk.demo.bean.FileItem;

public class FileListAdapter extends RecyclerView.Adapter  {

    private List<FileItem> list;
    private Context context ;
    private OnItemClickCallBack onItemClickCallBack ;

    public FileListAdapter(Context context ,OnItemClickCallBack onItemClickCallBack )  {
        this.context=context ;
        this.onItemClickCallBack = onItemClickCallBack;
    }

    public void updateData(DirFiles data){
        list= data.getmFileList() ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)  {
        LayoutInflater mInflater=LayoutInflater.from(context);
        View itemView=  mInflater.inflate(R.layout.fragment_file_list_item, parent, false) ;
        return new MyViewHolder(itemView );
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position)  {
        final MyViewHolder viewHolder = (MyViewHolder) holder;
        FileItem  bean = list.get(position) ;

        if (bean.getFile().isDirectory() ) {
            viewHolder.iconImageView.setImageResource(R.drawable.ic_theme_folder);
        }  else {
            viewHolder.iconImageView.setImageResource(R.drawable.ic_theme_description);
        }
        viewHolder.nameTextView.setText( bean.getFile().getName());
    }


    @Override
    public int getItemCount()  {
        return list != null ? list.size() : 0;
    }


    public interface OnItemClickCallBack {
        void onClick(File file) ;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView iconImageView;
        TextView nameTextView;

        MyViewHolder(View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = getAdapterPosition() ;
                    onItemClickCallBack.onClick(list.get(index ).getFile());
                }
            });
            iconImageView =  itemView.findViewById(R.id.icon);
            nameTextView =  itemView.findViewById(R.id.name);
        }
    }

}
